<template>
	<view class="wrap">
		<u-card class="task-list-item" :show-head="false" :border="true" padding="36" margin="24rpx"
			:head-border-bottom="false">
			<view slot="body">
				<u-row gutter="16" style="gap:24rpx">

					<u-col span="12">
						<view class="apply-text"><span>区域巡检单号：</span>{{code}}</view>
					</u-col>
					<u-col span="12">
						<view class="apply-text"><span>巡检时间：</span>{{this.checkTime}}</view>
					</u-col>
					<u-col span="12">
						<view class="apply-text"><span>巡检人：</span>{{this.checkPerson}}</view>
					</u-col>
				</u-row>
			</view>
		</u-card>
		<view>
			<!-- <u-line color="black" :hair-line="false" /> -->
			<view>
				<text style="color: cornflowerblue;font-size: x-large; margin-left: 20rpx;">巡检结果</text>
			</view>
			<view>
				<u-card class="task-list-item" :show-head="false" :border="true" padding="36" margin="24rpx"
					:head-border-bottom="false">
					<view slot="body">
						<u-row gutter="0" style="gap:24rpx">
							<u-col span="12">
								<view class="apply-text"><span>巡检水厂：</span>{{this.strucName}}</view>
							</u-col>
						</u-row>
					</view>
				</u-card>
			</view>
			<u-card v-for="item in submitInfo" :key="item.id" style="margin:24rpx" class="task-list-item"
				:show-head="false" :border="true" padding="28" margin="20" :head-border-bottom="false">
				<view class="resultBody" slot="body">
					<u-row gutter="16">
						<view class="apply-text"><text>{{item.item_name}}</text></view>
						<u-col @click="selectProject(item.id)" v-if="item.newData['项目'+item.id]" span="12"
							class="rowBody">
							<view class="apply-text item firstxkjhfx">
								<radio-group class="prijectsj" @change="selectItem">
									<label class="radio">
										<radio style="transform:scale(0.7)" value="1"
											:checked="item.newData['项目'+item.id]==='1'" />合格
									</label>
									<label class="radio">
										<radio style="transform:scale(0.7)" value="0"
											:checked="item.newData['项目'+item.id]==='0'" />不合格
									</label>
								</radio-group>
								<view class="prijectsjs">
									<text>备注：</text>
									<u-input v-model="item.newData['备注'+item.id]" type="text" :border="true" />
								</view>
							</view>
						</u-col>
						<u-col @click="selectProject(item.id)" v-else-if="item.newData['备注'+item.id]==''" span="12"
							class="rowBody">
							<view class="apply-text item firstxkjhfx">
								<radio-group class="prijectsj" @change="selectItem">
									<label class="radio">
										<radio style="transform:scale(0.7)" value="1"
											:checked="item.newData['项目'+item.id]==='1'" />合格
									</label>
									<label class="radio">
										<radio style="transform:scale(0.7)" value="0"
											:checked="item.newData['项目'+item.id]==='0'" />不合格
									</label>
								</radio-group>
								<view class="prijectsjs">
									<text>备注：</text>
									<u-input @blur="handleInput(item.id)" :ref="item.id"
										v-model="item.newData['备注'+item.id]" type="text" :border="true" />
								</view>
							</view>
						</u-col>
					</u-row>
				</view>

			</u-card>
			<view class="saveButton">
				<u-button :ripple="true" ripple-bg-color="#909399" @click="orderEdit()" size="medium"
					type="primary">保存</u-button>
			</view>
		</view>

	</view>
</template>
<script>
	export default {
		data() {
			return {
				list: [{
						name: 'apple',
						disabled: false
					},
					{
						name: 'banner',
						disabled: false
					},
					{
						name: 'orange',
						disabled: false
					}
				],
				checkOrder: "",
				checkTime: "",
				checkPerson: "",
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				value: '',
				orderId: null,
				code: null,
				orderInfo: null,
				orderResult: [],
				strucName: "",
				orderItems: null,
				inspection_code: "",
				inspection_date: "",
				structure_uuid: "",
				isHe: '',
				editId: '',
				submitData: [],
				contentVal: '',
				contentNum: '',
				subResult: null
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.orderId = option.id;
			this.code = option.code
			this.getGDDetail()
		},
		computed: {
			submitInfo() {
				return this.orderResult
			},
			// selectIsHeGe() {
			// 	return
			// }
		},
		methods: {
			//获取id，查询出工单详情
			getGDDetail() {
				this.$u.api.findQYXJGDDetail({
						"algorithmProcessId": "inspection_work_order_details",
						"param": {
							"data": {
								"id": this.orderId
							}
						}
					})
					.then(res => {
						let orderInfos = res.data.data.result || [];
						this.orderResult = orderInfos.items
						this.orderInfo = orderInfos;
						this.inspection_code = orderInfos.inspection_code;
						this.inspection_date = orderInfos.inspection_date;
						this.structure_uuid = orderInfos.structure_uuid;
						this.checkTime = this.orderInfo.inspection_date;
						this.checkPerson = this.orderInfo.nice_name;
						this.strucName = this.orderInfo.structure_name;
						let resultData = JSON.parse(orderInfos.inspection_result)


						for (var i = 0; i < this.orderResult.length; i++) {
							if (resultData != null) {
								for (var j = 0; j < resultData.length; j++) {
									var key = '项目' + this.orderResult[i].id;
									if (resultData[j].hasOwnProperty(key)) {
										this.orderResult[i].newData = resultData[j];
										break;
									}
								}
							}
							if (!this.orderResult[i].hasOwnProperty('newData')) {
								this.orderResult[i].newData = {
									['项目' + this.orderResult[i].id]: "",
									['备注' + this.orderResult[i].id]: ""
								};
							}
						}
						this.orderItems = resultData


					});
			},
			radioChange(e) {

			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {

			},
			selectItem(e) {

				this.isHe = e.detail.value
			},
			// selectItem1(e) {
			// 	console.log("未编辑", e)
			// 	this.isHe = e.detail.value
			// },
			//保存
			orderEdit() {
				//已经被编辑过的数据
				this.$u.api.editQYXJGDDetail1({
						"algorithmProcessId": "inspection_work_order_update",
						"param": {
							"data": {
								"id": this.orderId,
								"inspection_user": uni.getStorageSync('uuid'),
								"inspection_code": this.inspection_code,
								"inspection_date": this.inspection_date,
								"structure_uuid": this.structure_uuid,
								"inspection_result": this.subResult
							}
						}
					})
					.then(res => {
						uni.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 1000
						})
						setTimeout(() => {
							uni.switchTab({
								url: "/pages/sys/order/index",
							});
						}, 600);
					});


			},
			selectProject(id) {

				this.editId = id
				for (let i = 0; i < this.orderResult.length; i++) {
					if (this.orderResult[i].id == id) {
						this.orderResult[i].newData['项目' + id] = this.isHe
					}
					this.submitData.push(this.orderResult[i].newData)
				}
				//清除未编辑的数据
				this.submitData = this.submitData.filter(item => item["项目" + Object.keys(item)[0].slice(2)] !== "");
				//去重
				var result = [];
				var seen = {};
				for (var i = 0; i < this.submitData.length; i++) {
					var item = this.submitData[i];
					var key = Object.keys(item)[0];

					if (!seen[key]) {
						result.push(item);
						seen[key] = true;
					}
				}
				this.subResult = result
			},

			handleInput(f) {
				this.contentNum = f;
				console.log("备注", f, this.$refs[f])
				var a = this.$refs[f] || [];
				this.contentVal = a[0].defaultValue || ''

				//加载数据
				for (let i = 0; i < this.orderResult.length; i++) {
					if (this.orderResult[i].id == f) {
						//this.orderResult[i].newData['项目' + this.contentNum] = this.isHe
						this.orderResult[i].newData['备注' + this.contentNum] = this.contentVal

					}
					this.submitData.push(this.orderResult[i].newData)
				}
				//清除未编辑的数据
				this.submitData = this.submitData.filter(item => item["项目" + Object.keys(item)[0].slice(2)] !== "");
				//去重
				var result = [];
				var seen = {};

				for (var i = 0; i < this.submitData.length; i++) {
					var item = this.submitData[i];
					var key = Object.keys(item)[0];
					if (!seen[key]) {
						result.push(item);
						seen[key] = true;
					}
				}
				this.subResult = result
			},

		},
	}
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.wrap .search {
		background: #ffffff;
	}

	.uni-title {
		font-size: 30rpx;
		color: #333333;
		padding: 20rpx;
		background: #fff;
		border-top: 2rpx solid #ededed;
		margin-top: 20rpx;
	}

	.uni-media-list {
		padding: 30rpx;
	}

	.uni-media-list-body {
		height: 84rpx;
		padding-left: 20rpx;
	}

	.uni-media-list-text-top {
		height: 40rpx;
		overflow: hidden;
		width: 100%;
		line-height: 40rpx;
		font-size: 32rpx;
		display: flex;
		justify-content: space-between;
	}

	.uni-media-list-text-bottom {
		width: 100%;
		line-height: 24rpx;
		font-size: 26rpx;
		color: #666666;

		span {
			margin-right: 10rpx;
		}
	}


	.head-text {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		padding: 30rpx 0;
		margin-left: 24rpx;
	}



	.grid {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 24rpx;

		.grid-text {
			font-size: 28rpx;
			margin-top: 28rpx;
			color: $u-type-info;
			// color: #333;
			font-weight: bold;
		}

	}

	.charts-box {
		width: 100%;
		height: 600rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 24rpx;
	}

	.apply-text {
		font-size: 28rpx;
		color: #333333;
		height: 50rpx;

		span {
			color: #999999;
		}
	}


	.apply-list-foot {
		font-size: 28rpx;
	}


	.searchButton {
		height: 50rpx;
	}

	.selectCondition {

		.search {
			display: inline-block;
			width: 300rpx;

		}

		.selectItem {
			display: contents;
		}

	}

	.wrap .navProgramm {
		white-space: nowrap;

		.item {
			width: 50%;
			height: 220rpx;
			display: inline-block;
			margin-right: 10rpx;
		}
	}

	.rowBody {
		display: flex;
		justify-content: space-between;

		.item {
			flex: 1;
			display: flex;
			justify-content: space-between;
		}
	}

	.firstxkjhfx {
		display: flex;
		align-items: center;

		.prijectsjs {
			width: 60%;
			display: flex;
			align-items: center;
			justify-content: center;

		}
	}

	.saveButton {
		text-align: center;
	}

	.task-list-item {
		box-shadow: 4rpx -4rpx 6rpx rgba(0, 0, 0, 0.1) !important;
	}
</style>